#!/bin/bash
#
# init script for arptables
#
# Original by Dag Wieers <dag@wieers.com>.
# Modified/changed to arptables by
# 	Rok Papez <rok.papez@arnes.si>.
#
# chkconfig: - 16 84
# description: Arp filtering tables
#
# config: __SYSCONFIG__/arptables

source /etc/init.d/functions
source /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x __EXEC_PATH__/arptables ] || exit 1
[ -x __EXEC_PATH__/arptables-save ] || exit 1
[ -x __EXEC_PATH__/arptables-restore ] || exit 1

[ "$1" != "save" -o -r __SYSCONFIG__/arptables ] || exit 1

RETVAL=0
prog="arptables"
desc="Arp filtering"

start() {
	echo -n $"Starting $desc ($prog): "
	__EXEC_PATH__/arptables-restore < __SYSCONFIG__/arptables || RETVAL=1

	if [ $RETVAL -eq 0 ]; then
		success "$prog startup"
		rm -f /var/lock/subsys/$prog
	else
		failure "$prog startup"
	fi

	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping $desc ($prog): "
	__EXEC_PATH__/arptables-restore < /dev/null || RETVAL=1

	if [ $RETVAL -eq 0 ]; then
		success "$prog shutdown"
		rm -f %{_localstatedir}/lock/subsys/$prog
	else
		failure "$prog shutdown"
	fi

	echo
	return $RETVAL
}

restart() {
	stop
	start
}

save() {
	echo -n $"Saving $desc ($prog): "
	__EXEC_PATH__/arptables-save > __SYSCONFIG__/arptables || RETVAL=1

	if [ $RETVAL -eq 0 ]; then
		success "$prog saved"
	else
		failure "$prog saved"
	fi
	echo
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  condrestart)
	[ -e /var/lock/subsys/$prog ] && restart
	RETVAL=$?
	;;
  save)
	save
	;;
  status)
	__EXEC_PATH__/arptables-save
	RETVAL=$?
	;;
  *)
	echo $"Usage $0 {start|stop|restart|condrestart|save|status}"
	RETVAL=1
esac

exit $RETVAL
